import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_note/db/db_helper.dart';
import 'package:flutter_note/entity/note.dart';
import 'package:flutter_note/utils/sys_utils.dart';
import 'package:logger/logger.dart';
import 'package:oktoast/oktoast.dart';

class NoteEditPage extends StatefulWidget {
  final Map params;

  NoteEditPage({this.params});

  @override
  State<StatefulWidget> createState() {
    return _NoteEditPageState();
  }
}

class _NoteEditPageState extends State<NoteEditPage> {
  var logger = Logger();
  TextEditingController _controller = TextEditingController();

  Note note;

  @override
  void initState() {
    if (widget.params != null) {
      // logger.d("note-------------->${jsonEncode(widget.params)}");
      note = Note.fromMap(widget.params);
      _controller.text = note.content;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.done),
            onPressed: () {
              if (note == null || note.id == null) {
                _insert(context, _controller.text);
              } else {
                _update(context, note, _controller.text);
              }
            },
          )
        ],
        title: Text(
          '编辑',
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: Container(
        child: TextField(
          maxLines: 15,
          autofocus: true,
          keyboardType: TextInputType.text,
          textInputAction: TextInputAction.done,
          cursorColor: Theme.of(context).primaryColor,
          controller: _controller,
          decoration: InputDecoration(
              //  labelText: note?.content,
              hintText: '请输入内容',
              hintStyle: TextStyle(fontSize: 16, color: Colors.grey),
              labelStyle: TextStyle(fontSize: 16, color: Colors.black),
              contentPadding: EdgeInsets.all(12),
              border: InputBorder.none),
        ),
      ),
    );
  }

  void _insert(BuildContext context, String text) {
    Note note = Note();
    // note.id = 120;
    note.content = text;
    note.ts = System.currentTimeMillis();

    DbHelper.instance.insert(note).then((Note note) {
      showToast('id:${note.id}');
      // Provider.of<NoteProvide>(context).insert(note);
      Navigator.of(context).pop(note);
    }, onError: (e) => logger.e(e.toString()));

    /*
    *  showToast('id:${note.id}');
        Provider.of<NoteProvide>(context).insert(note);
    // Navigator.of
    * */
  }

  void _update(BuildContext context, Note oldNote, String text) {
    oldNote.content = text;
    note.ts = System.currentTimeMillis();

    DbHelper.instance.update(note).then((value) {
      logger.d("update--------------->$value");
      Navigator.of(context).pop(value);
    }, onError: (e) => showToast(e.toString()));
  }
}
